/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.graph;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides the implementation of imaga drawing-related primitives.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Image {

    /**
     * No instance of this class.
     */
    private Image() {
    } // end empty constructor

    /**
     * Creates an image.
     * @param ctxt context
     * @param vw image width
     * @param vh image height
     * @return created image
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_create_image(final CodeRunner ctxt,
                                             final Value vw,
                                             final Value vh)
        throws Fail.Exception {
        GraphSlot.checkGraph(ctxt);
        final int w = vw.asLong();
        final int h = vh.asLong();
        final BufferedImage img =
            new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g = img.createGraphics();
        g.setColor(java.awt.Color.WHITE);
        g.fillRect(0, 0, w, h);
        g.dispose();
        final Block res = Block.createCustom(CustomImage.SIZE,
                                             CustomImage.OPS);
        res.setCustom(img);
        return Value.createFromBlock(res);
    } // end method 'caml_gr_create_image(CodeRunner, Value, Value)'

    /**
     * Copies screen pixels from the frame into an image.
     * @param ctxt context
     * @param vimg destination image
     * @param vx x-component of the lower left corner of copied rectangle
     * @param vy y-component of the lower left corner of copied rectangle
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_blit_image(final CodeRunner ctxt,
                                           final Value vimg,
                                           final Value vx,
                                           final Value vy)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final BufferedImage src = slot.getBuffer();
        final BufferedImage img = (BufferedImage) vimg.asBlock().asCustom();
        final int w = img.getWidth();
        final int h = img.getHeight();
        final Graphics2D g = img.createGraphics();
        g.drawImage(src.getSubimage(vx.asLong(),
                                    slot.convertY(vy.asLong()) - h,
                                    w,
                                    h),
                    0,
                    0,
                    null);
        g.dispose();
        return Value.UNIT;
    } // end method 'caml_gr_blit_image(CodeRunner, Value, Value, Value)'

    /**
     * Copies screen pixels from an image into the frame.
     * @param ctxt context
     * @param vimg source image
     * @param vx x-component of the lower left corner of copied rectangle
     * @param vy y-component of the lower left corner of copied rectangle
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_draw_image(final CodeRunner ctxt,
                                           final Value vimg,
                                           final Value vx,
                                           final Value vy)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final BufferedImage img = (BufferedImage) vimg.asBlock().asCustom();
        final int w = img.getWidth();
        final int h = img.getHeight();
        final int x = vx.asLong();
        final int y = slot.convertY(vy.asLong()) - h;
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.drawImage(img, x, y, w, h, null);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.drawImage(img, x, y, w, h, null);
            store.dispose();
        } // end if
        return Value.UNIT;
    } // end method 'caml_gr_draw_image(CodeRunner, Value, Value, Value)'

} // end class 'Image'
